using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Evolution.Engine.Objects;

namespace Evolution.Engine.Managers
{
    public class LightManager : Singleton<LightManager>, Evolution.Engine.Interfaces.IUpdateable
    {
        private int _MaxLights;
        private List<Light> _Lights = new List<Light>();

        private List<Vector3> _Positions = new List<Vector3>();
        private List<Vector4> _Specular = new List<Vector4>();
        private List<Vector4> _Diffuse = new List<Vector4>();
        private List<Vector4> _Ambient = new List<Vector4>();
        private List<float> _Power = new List<float>();
        private List<int> _Type = new List<int>();
        private List<Light> _ActiveLights = new List<Light>();

        public List<Light> ActiveLights { get { return _ActiveLights; } }
        public List<int> Type { get { return _Type; } }
        public List<float> Power { get { return _Power; } }
        public List<Vector4> Ambient { get { return _Ambient; } }
        public List<Vector4> Diffuse { get { return _Diffuse; } }
        public List<Vector4> Specular { get { return _Specular; } }
        public List<Vector3> Positions { get { return _Positions; } }
        public List<Light> Lights { get { return _Lights; } }
        public int MaxLights { get { return _MaxLights; } set { _MaxLights = value; } }

        public void Add(Light light)
        {
            if (!_Lights.Contains(light))
                return;

            _Lights.Add(light);
        }

        public void Update(GameTime gameTime)
        {
            ClearActiveLights();

            for (int i = 0; i < _Lights.Count; i++)
            {
                if (_Lights[i].Visible)
                {
                    _Positions.Add(_Lights[i].Position);
                    _Specular.Add(_Lights[i].Specular);
                    _Diffuse.Add(_Lights[i].Diffuse);
                    _Ambient.Add(_Lights[i].Ambient);
                    _Power.Add(_Lights[i].Power);
                    _Type.Add((int)_Lights[i].Type);
                    _ActiveLights.Add(_Lights[i]);
                }
            }
        }

        private void ClearActiveLights()
        {
            if (_ActiveLights != null) _ActiveLights.Clear();
            if (_Positions != null) _Positions.Clear();
            if (_Specular != null) _Specular.Clear();
            if (_Diffuse != null) _Diffuse.Clear();
            if (_Ambient != null) _Ambient.Clear();
            if (_Power != null) _Power.Clear();
            if (_Type != null) _Type.Clear();
        }
    }
}
